package com.easywan.web.controller.admin;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.easywan.core.JSPModelAndView;
import com.easywan.core.RedirectModelAndView;
import com.easywan.service.IBendibaoService;
import com.easywan.service.IWanActiveService;
import com.easywan.util.Utils;
import com.easywan.vo.Bendibao;
import com.easywan.vo.Pages;
import com.easywan.vo.WanActive;

@Controller
@RequestMapping("admin/Bendibao")
public class BendibaoControllerAdmin {
	private static Logger logger = Logger.getLogger(BendibaoControllerAdmin.class);
	@Autowired
	private IWanActiveService wanActiveService;
	@Autowired 
	IBendibaoService service;
	@RequestMapping("index")
	public ModelAndView index(HttpServletRequest request,HttpServletResponse response,Pages pages,
			@RequestParam("pname")String pname,
			@RequestParam("name")String name){
		JSPModelAndView view = new JSPModelAndView("/admin/Bendibao/index");
		
		if(pages.getPage() < 1){
			pages.setPage(1);
		}
		
		List<Bendibao> lists = service.list(pages);
		view.addObject("data",lists);
		view.addObject("pname", pname);
		view.addObject("name", name);
		view.addObject("page", pages);
		return view;
	}
	@RequestMapping("cj")
	public void cj(HttpServletRequest request,HttpServletResponse response,
			Bendibao bendibao){
		WanActive wan = new WanActive();
		wan.setTitle(bendibao.getTitle());
		if(Utils.listIsEmpty(wanActiveService.listBySelectTive(wan))){
			String content = service.get(bendibao);
			wan.setAddress(bendibao.getAddress());
			wan.setAgrees(0);
			wan.setContent(content);
			wan.setCover(bendibao.getCoverHref());
			wan.setCreatetime(Utils.getNowDate());
			wan.setLooks(0);
			wan.setModifytime(Utils.getNowDate());
			wan.setPiao(bendibao.getPiao());
			wan.setSource(bendibao.getSource());
			wan.setStartTime(bendibao.getTime());
			
			wanActiveService.add(wan);
			Utils.sendMsg(response, "1");
			return;
		}
		Utils.sendMsg(response, "0");
	}
}
